<?php


namespace mpend\modules\actor\controllers;

use Yii;
use common\models\actor\Actor;
use common\models\actor\ActorTag;
use common\models\actor\Taglib;
use mpend\components\Controller;
use mpend\modules\actor\models\TaglibForm;

class TaglibController extends Controller
{
    /**
     * @inheritdoc
     */
    protected function verbs()
    {
        return [
            '*' => ['POST'],
        ];
    }

    public function actionIndex()
    {
//        $model = new TaglibForm();
//        return $model->taglist();
        $model = Taglib::find()->all();
        return $model;
    }

    /**
     * 设置简历技能
     * @return array|string|void|null
     * @throws \common\base\ModelHttpException
     * @throws \yii\base\InvalidConfigException
     */
    public function actionActorTag()
    {
        $model = new ActorTag();
        $params = Yii::$app->request->post();
        $tagmodel = Taglib::find()->active()->andWhere(['id' => $params['id']])->one();
        $actor = Actor::find()->active()->andWhere(['user_id' => $this->uid])->one();
        if (!$actor) {
            return $model->dumpError('用户简历不存在');
        }
        $model->setAttributes([
            'actor_id' => $actor->id,
            'taglib_id' => $tagmodel->id,
            'text' => $tagmodel->name
        ]);
        if (!$model->save()) {
            return $model->getErrorMsg();
        }
        return $model->toArray();
    }

    /**
     * 设置简历自定义技能
     */
    public function actionCustomTag()
    {
        $actormodel = Actor::find()->active()->andWhere(['user_id' => $this->uid])->one();
        $model = new ActorTag();
        $model->setAttributes([
            'actor_id' => $actormodel->id,
            'taglib_id' => '0',
            'text' => Yii::$app->request->post('tagname')
        ]);
        if (!$model->save()) {
            return $model->getErrorMsg();
        }
        $datamodel = ActorTag::find()->active()->andWhere([
                'actor_id' => $actormodel->id,
                'taglib_id' => 0
            ]
        )->all();
        return $datamodel;
    }

    /**
     * 删除简历自定义技能
     */
    public function actionDelCustomTag()
    {
        $model = ActorTag::find()->active()->andWhere(['text' => Yii::$app->request->post('tagname')])->one();
        if ($model) {
            $model->delete();
        }
        $actormodel = Actor::find()->active()->andWhere(['user_id' => $this->uid])->one();
        $datamodel = ActorTag::find()->active()->andWhere([
                'actor_id' => $actormodel->id,
                'taglib_id' => 0
            ]
        )->all();
        return $datamodel;
    }

    /**
     * 删除简历技能
     * @return array|void
     * @throws \Throwable
     * @throws \common\base\ModelHttpException
     * @throws \yii\base\InvalidConfigException
     * @throws \yii\db\Exception
     * @throws \yii\db\StaleObjectException
     */
    public function actionDelActorTag()
    {
        $id = Yii::$app->request->post('id');
        $model = ActorTag::find()->active()->andWhere(['taglib_id' => $id])->one();
        if (!$model) {
            return $model->dumpError('该简历技能不存在');
        }
        $model->delete();
        return [];
    }
}